package com.woniu.punishcomplaintnotice.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.woniu.common.entity.PageResult;
import com.woniu.common.entity.PunishmentRecord;
import com.woniu.common.entity.User;
import com.woniu.punishcomplaintnotice.openfeignclient.UserOpenfeignClient;
import com.woniu.punishcomplaintnotice.service.PunishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@CrossOrigin
@RestController
public class PunishController {

    @Autowired
    PunishService punishService;
    @Autowired
    UserOpenfeignClient userOpenfeignClient;

    //查询全部处罚
    @RequestMapping("/findAllPunishment")
    public PageResult<PunishmentRecord> findAllPunishment(Integer page,Integer size){
        return punishService.findAllPunishment(page, size);
    }

    //分页查询处罚记录
    @GetMapping("/punishment")
    public PageResult<PunishmentRecord> findPunishmentByUid(Integer u_id, Integer page, Integer size){

        return punishService.findPunishmentByUid(u_id, page, size);

    }

    @RequestMapping(value = "uploadPunish",method = RequestMethod.POST)
    public void uploadPunish(HttpServletRequest request, @RequestParam MultipartFile file){

        String name = request.getParameter("name");
        String address = request.getParameter("address");
        //String times = request.getParameter("times");
        String actives = request.getParameter("actives");
        String results = request.getParameter("results");
        //

        String url=null;
        // yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1（杭州）为例，Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
        String endpoint = "oss-cn-chengdu.aliyuncs.com";
        // 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
        String accessKeyId = "LTAI5tRST65ssEbSEwikZfCN";
        String accessKeySecret = "dqc3VgpVkBnEx9q7iQ8bNpXt36iutI";
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try {
            // 填写本地文件的完整路径。如果未指定本地路径，则默认从示例程序所属项目对应本地路径中上传文件流。
            InputStream inputStream = file.getInputStream();
            //照片或者文件名字
            String filename =file.getOriginalFilename();
            //文件重名
            filename= UUID.randomUUID().toString();
            //文件打散
            DateFormat df= new SimpleDateFormat("yyyy/MM/dd");
            String strDate = df.format(new Date());
            filename=strDate+"/"+filename;
            // 填写Bucket名称和Object完整路径。Object完整路径中不能包含Bucket名称。
            ossClient.putObject("poppyflower", filename, inputStream);
            // 关闭OSSClient。
            ossClient.shutdown();
            //返回图片或者文件在OSS里面的路径
            //https://poppyflower.oss-cn-chengdu.aliyuncs.com/DogRegister/flowChart.png
            url = "https://poppyflower.oss-cn-chengdu.aliyuncs.com/"+filename;
            /*responseResult.setCode(200);
            responseResult.setMessage("上传成功");
            responseResult.setData(url);*/

        } catch (IOException e) {
            e.printStackTrace();
        }



            PunishmentRecord punishmentRecord=new PunishmentRecord();
            punishmentRecord.setName(name);
            punishmentRecord.setPhoto(url);
            Date times=new Date();
            System.out.println(times);
            punishmentRecord.setPunish_date(times);
            punishmentRecord.setResult(results);
            punishmentRecord.setActive(actives);
            punishmentRecord.setAddress(address);

            User user=userOpenfeignClient.selectUser(name);
            int uid=user.getId();
            punishmentRecord.setU_id(uid);
            punishService.insertPunish(punishmentRecord);

    }
}
